Implement orthogonal vector decomposition. Start with two random-number vectors t and r, and reproduce Figure 2-8 (note that your plot will look somewhat different due to random numbers). Next, confirm that the two components sum to t and that t⊥r and t∥r are orthogonal.
import numpy as np
import matplotlib.pyplot as plt
# Generate random target and reference vectors
target_vec = np.random.randint(1, 7, 2) # Generates random integers between 1 and 6 for each component
ref_vec = np.random.randint(1, 7, 2)
# Print vectors for verification
print("Target Vector:", target_vec)
print("Reference Vector:", ref_vec)
Target Vector: [6 1] Reference Vector: [4 3]
# Calculate the parallel component of target_vec to ref_vec
parallel_vec = ref_vec * (np.dot(target_vec, ref_vec) / np.dot(ref_vec, ref_vec))
print("Parallel Component:", parallel_vec)
Parallel Component: [4.32 3.24]
# Calculate the perpendicular component of target_vec to ref_vec
perp_vec = target_vec - parallel_vec
print("Perpendicular Component:", perp_vec)
Perpendicular Component: [ 1.68 -2.24]
# Setup the plot
plt.figure(figsize=(8, 8))
plt.grid()
# Define the origin
origin = np.array([0, 0])
arrow_options = dict(linewidth=0.2, head_width=0.2)
# Draw arrows for target and reference vectors
plt.arrow(*origin, *target_vec, color='r', **arrow_options)
plt.arrow(*origin, *ref_vec, color='g', **arrow_options)
# Add labels to the target and reference vectors
target_label_pos = target_vec + np.array([0.2, 0.2])
ref_label_pos = ref_vec + np.array([0.2, 0.2])
plt.text(*target_label_pos, s=r'$\vec{T}$')
plt.text(*ref_label_pos, s=r'$\vec{R}$')
# Draw arrows for parallel and perpendicular components
plt.arrow(*origin, *parallel_vec, color='b', **arrow_options)
plt.arrow(*origin, *perp_vec, color='y', **arrow_options)
# Label the parallel and perpendicular components
parallel_label_pos = parallel_vec + np.array([0.2, 0.2])
perp_label_pos = perp_vec + np.array([0.2, 0.2])
plt.text(*parallel_label_pos, s=r'$\vec{T}_{\parallel \vec{R}}$')
plt.text(*perp_label_pos, s=r'$\vec{T}_{\perp \vec{R}}$')
# Set axis properties to keep the aspect ratio equal
plt.axis('equal')
plt.show()
# Check orthogonality by calculating dot product (should be close to 0)
dot = np.dot(perp_vec, parallel_vec)
print("Dot Product (should be 0):", dot)
Dot Product (should be 0): -2.0051516003150028e-15
# Verify if the sum of the components equals the original vector
sum_of_comps = parallel_vec + perp_vec
print("Sum of Components:", sum_of_comps)
print("Original Target Vector:", target_vec)
Sum of Components: [6. 1.] Original Target Vector: [6 1]
The exercise involves implementing orthogonal vector decomposition. This method is used to break down a vector r into two orthogonal components relative to another vector t: one component parallel (t∥) and one component perpendicular (t⊥) to t. The goal is to visualize these components and confirm their orthogonality.
In this exercise, you will draw random points in subspaces. This will help reinforce the idea that subspaces comprise any linear weighted combination of the spanning vectors. Define a vector set containing one vector [1, 3, 3]. Then create 100 numbers drawn randomly from a uniform distribution between -4 and +4. Those are your random scalars. Multiply the random scalars by the basis vector to create 100 random points in the subspace. Plot those points.
Next, repeat the procedure but using two vectors in R3 : [3, 5, 1] and [0, 2, 2]. Note that you need 100 x 2 random scalars for 100 points and two vectors. The resulting random dots will be on a plane. Figure 3-7 shows what the results will look like (it’s not clear from the figure that the points lie on a plane, but you’ll see this when you drag the plot around on your screen).
I recommend using the plotly library to draw the dots, so you can click-drag the 3D axis around. Here’s a hint for getting it to work:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Scatter3d(
x=points[:,0], y=points[:,1], z=points[:,2],
mode='markers')])
fig.show()
Finally, repeat the R3 case but setting the second vector to be 1/2 times the first.
import numpy as np
import plotly.graph_objects as go
# Part 1: Single vector [1, 3, 3]
vector1 = np.array([1, 3, 3])
scalars1 = np.random.uniform(-4, 4, 100)
points1 = scalars1[:, np.newaxis] * vector1
# Create the plot for the single vector
fig1 = go.Figure(data=[go.Scatter3d(
x=points1[:, 0],
y=points1[:, 1],
z=points1[:, 2],
mode='markers',
marker=dict(size=4)
)])
fig1.update_layout(title="3D Scatter Plot of Points Along Single Vector [1, 3, 3]")
fig1.show()
# Part 2: Two vectors [3, 5, 1] and [0, 2, 2]
vector2a = np.array([3, 5, 1])
vector2b = np.array([0, 2, 2])
scalars2a = np.random.uniform(-4, 4, 100)
scalars2b = np.random.uniform(-4, 4, 100)
points2 = scalars2a[:, np.newaxis] * vector2a + scalars2b[:, np.newaxis] * vector2b
# Create the plot for two vectors
fig2 = go.Figure(data=[go.Scatter3d(
x=points2[:, 0],
y=points2[:, 1],
z=points2[:, 2],
mode='markers',
marker=dict(size=4)
)])
fig2.update_layout(title="3D Scatter Plot of Points on a Plane Formed by Vectors [3, 5, 1] and [0, 2, 2]")
fig2.show()
# Part 3: Two vectors with second vector being 1/2 times the first
vector3a = vector2a
vector3b = 0.5 * vector2a
scalars3a = np.random.uniform(-4, 4, 100)
scalars3b = np.random.uniform(-4, 4, 100)
points3 = scalars3a[:, np.newaxis] * vector3a + scalars3b[:, np.newaxis] * vector3b
# Create the plot for modified two vectors
fig3 = go.Figure(data=[go.Scatter3d(
x=points3[:, 0],
y=points3[:, 1],
z=points3[:, 2],
mode='markers',
marker=dict(size=4)
)])
fig3.update_layout(title="3D Scatter Plot of Points on a Line, Second Vector 1/2 the First")
fig3.show()
# Part 1: Single vector [1, 3, 3]
vector1 = np.array([1, 3, 3])
scalars1 = np.random.uniform(-4, 4, 100)
points1 = scalars1[:, np.newaxis] * vector1
# Create the plot for the single vector
fig1 = go.Figure(data=[go.Scatter3d(
x=points1[:, 0],
y=points1[:, 1],
z=points1[:, 2],
mode='markers',
marker=dict(size=4)
)])
fig1.update_layout(title="3D Scatter Plot of Points Along Single Vector [1, 3, 3]")
In the exercise, we needed to understand the concept of subspaces in vector spaces by visualizing random points that align with given vectors in R3. The goal is to see how linear combinations of vectors form subspaces such as lines and planes.
1. Single Vector [1, 3, 3]:
- Defined a vector and generated 100 random scalars.
- Computed points as linear combinations of the vector scaled by these random values.
- Plotted these points to visualize a line in three-dimensional space.
2. Two Vectors [3, 5, 1] and [0, 2, 2]:
- Used two vectors and generated 200 random scalars (100 for each vector).
- Computed points as linear combinations of these vectors using the random scalars.
- Plotted these points to observe the formation of a plane in three-dimensional space.
3. Modified Two Vectors Case:
- Repeated the two-vector scenario but with the second vector being half the first vector.
- This was meant to show that the points still lie on a line despite using two vectors, due to the linear dependency between the vectors.
Random Scalars: Generated using np.random.uniform(-4, 4, 100), which produces numbers between -4 and 4.
Vectors:
- For the line: [1,3,3]
- For the plane: [3,5,1] and [0,2,2]
- For the line with dependent vectors: [3,5,1] and [1.5,2.5,0.5] (which is half of the first vector).
3D Scatter Plots: These plots visually represent the points in the subspace formed by the given vectors. The plots allow interaction such as rotation to view the alignment of points in 3D space.
Below You will be able to find Github Link to my week 2 lab assignment
https://github.com/kirtishrestha/MTH225
To see the output figures please run the cell block.